package com.zlq.servlet;

import com.zlq.dao.UserMapper;
import com.zlq.entity.User;
import com.zlq.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //PrintWriter out = response.getWriter();
        try {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");


            SqlSession sqlSession = MybatisUtils.getSqlSession();

            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //User userByUserName = mapper.getUserByUserName("zlq");
            //System.out.println(userByUserName);


            //Object user_count = request.getSession().getAttribute("User_Count");

//            request.getSession().setAttribute("User_Count",0);
//            Integer user_count = (Integer) request.getSession().getAttribute("User_Count");
//            int count = user_count.intValue();
//            user_count = new Integer(count + 1);
//            User user = new User();
//            user.setUserName("root");
//            user.setPassword("12345");

            String inUserName = request.getParameter("inUserName");
            String inUserPwd = request.getParameter("inUserPwd");

//            String userName = user.getUserName();
//            String password = user.getPassword();

            User userByUserName = mapper.getUserByUserName(inUserName);
            System.out.println(userByUserName);

            if (userByUserName == null){
                response.sendRedirect("/login.jsp");
            }
            String userName1 = userByUserName.getUserName();
            String password1 = userByUserName.getPassword();


            //getServletContext().getRequestDispatcher("/loginSuccession.jsp").forward(request,response);
            if (inUserName.equals(userName1) && inUserPwd.equals(password1)){

                request.getSession().setAttribute("USER_SESSION",request.getSession().getId());
//                request.getSession().setAttribute("User_Count",1);
//                request.getSession().setAttribute("User_Count",user_count);

                //getServletContext().setAttribute("User_Count",user_count);
                getServletContext().getRequestDispatcher("/sys/loginSuccession.jsp").forward(request,response);
                //out.println("Login Success!");
            }else {
                out.println("Login Fail!");
                response.sendRedirect("/login.jsp");
            }

            sqlSession.close();
            out.close();

        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
